package khs.java_pdf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFImageWriter;

/**
 * Hello world!
 *
 */
public class App 
{
    private static BufferedReader bufferedReader;
    
    public static void main( String[] args ) throws IOException
    {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        String input_mode;
        do
        {
            fnPrintMode();
            input_mode = bufferedReader.readLine();
            
            switch(input_mode)
            {
            case "1":
                fnPdf2PngMode();
                break;
            }
        }while(!input_mode.equals("q"));
        
        bufferedReader.close();
    }
    
    public static void fnPrintMode()
    {
        System.out.println("1. pdf2png");
        System.out.println("q. quit");
    }
    
    public static void fnPdf2PngMode() throws IOException
    {
        System.out.print("input pdf: ");
        String input_pdf = bufferedReader.readLine();
        
        System.out.print("input prefix: ");
        String input_prefix = bufferedReader.readLine();
        
        fnPdf2Png(input_pdf, input_prefix);
    }
    
    public static void fnPdf2Png(String pdf, String input_prefix) throws IOException
    {
        PDDocument pdDocument = PDDocument.load(pdf);
        PDFImageWriter pdfImageWriter = new PDFImageWriter();
        System.out.println(pdfImageWriter.writeImage(pdDocument, "png", null, 1, Integer.MAX_VALUE, input_prefix));
    }
}
